module.exports = Behavior({
  methods: {
    observe(cb: Function, target: String, container?: String): void {
      if (this._observer) this._observer.disconnect();
      // 初始化监听器
      this._observer = wx.createIntersectionObserver(this, {
        observeAll: true
      })
      // 监听元素
      if (container) {
        this._observer.relativeTo(container).observe(target, (res) => {
          if (res.intersectionRatio > 0) cb(res)
        })
      } else {
        this._observer.relativeToViewport().observe(target, (res) => {
          if (res.intersectionRatio > 0) cb(res)
        })
      }

    },
  },
  onUnload() {
    if (this._observer) this._observer.disconnect();
  }
})